package com.zl.quickdoc.resolver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * java类型定义
 */
public class JavaTypeRegistrar {
    
    private static final Map<String, String> simpleTypeHandler = new LinkedHashMap<>();
    private static final List<String> fileTypeHandler = new ArrayList<>();
    private static final List<String> listTypeHandler = new ArrayList<>();
    private static final List<String> mapTypeHandler = new ArrayList<>();
    private static final List<String> ignoreClassHandler = new ArrayList<>();
    private static final Map<String,String> typeConvertMap = new HashMap<>();
    
    static {
        simpleTypeHandler.put("String", "String");
        simpleTypeHandler.put("Character", "String");
        simpleTypeHandler.put("char", "String");
        
        simpleTypeHandler.put("int", "Integer");
        simpleTypeHandler.put("long", "Integer");
        simpleTypeHandler.put("short", "Integer");
        simpleTypeHandler.put("BigInteger", "Integer");
        simpleTypeHandler.put("Integer", "Integer");
        
        simpleTypeHandler.put("float", "Number");
        simpleTypeHandler.put("double", "Number");
        simpleTypeHandler.put("BigDecimal", "Number");
        simpleTypeHandler.put("boolean", "Boolean");
        
        simpleTypeHandler.put("Date", "String");
        simpleTypeHandler.put("Instant", "String");
        simpleTypeHandler.put("LocalDate", "String");
        simpleTypeHandler.put("LocalDateTime", "String");
        simpleTypeHandler.put("LocalTime", "String");
        simpleTypeHandler.put("timestamp", "String");
        simpleTypeHandler.put("time", "String");
        simpleTypeHandler.put("OffsetDateTime", "String");
        simpleTypeHandler.put("OffsetTime", "String");
        simpleTypeHandler.put("ZonedDateTime", "String");
        simpleTypeHandler.put("Month", "String");
        simpleTypeHandler.put("Year", "String");
        simpleTypeHandler.put("YearMonth", "String");
        simpleTypeHandler.put("JapaneseDate", "String");

        fileTypeHandler.add("InputStream");
        fileTypeHandler.add("MultipartFile");
        
        listTypeHandler.add("Set");
        listTypeHandler.add("List");
        listTypeHandler.add("Iterable");
        listTypeHandler.add("Collection");
        listTypeHandler.add("ArrayList");
        
        
        mapTypeHandler.add("Map");
        mapTypeHandler.add("HashMap");
        mapTypeHandler.add("LinkedHashMap");
        
        ignoreClassHandler.add("org.apache.catalina.*");
        ignoreClassHandler.add("javax.servlet.*");
        ignoreClassHandler.add("org.springframework.*");
        
        typeConvertMap.put("Number", "数值");
        typeConvertMap.put("Integer", "整数");
        typeConvertMap.put("String", "字符串");
        typeConvertMap.put("Objects", "对象");
        typeConvertMap.put("Arrays", "集合对象");
        typeConvertMap.put("File", "文件流对象");
        typeConvertMap.put("Map", "对象");
        typeConvertMap.put("None", "未知类型");
        typeConvertMap.put("*", "字符串");
    }
    
    public static String getDefSimpleType() {
        return "String";
    }
    /**
     * 是否非字符
     */
    public static boolean checkNotString(String typeName) {
        return "Boolean".equals(typeName) || "Number".equals(typeName) || "Integer".equals(typeName);
    }
    /**
     * 获取显示类型
     */
    public static String getShowType(String typeName) {
        for (Map.Entry<String, String> entry : typeConvertMap.entrySet()) {
            if (entry.getKey().equalsIgnoreCase(typeName)) {
                return entry.getValue();
            }
        }
        return null;
    }
    public static void putAllShowType(Map<String,String> types) {
        typeConvertMap.putAll(types);
    }
    /**
     * 获取简单类型
     */
    public static String getSimpleType(String typeName) {
        for (Map.Entry<String, String> entry : simpleTypeHandler.entrySet()) {
            if (entry.getKey().equalsIgnoreCase(typeName)) {
                return entry.getValue();
            }
        }
        return null;
    }
    public static void putSimpleType(Map<String,String> types) {
        simpleTypeHandler.putAll(types);
    }
    
    /**
     * 获取文件类型
     */
    public static boolean checkFileType(String typeName) {
        for (String key : fileTypeHandler) {
            if (key.equalsIgnoreCase(typeName) || key.endsWith(typeName)) {
                return true;
            }
        }
        return false;
    }
    /**
     * 注册文件类型
     */
    public static void putFileType(String typeName) {
        fileTypeHandler.add(typeName);
    }
    
    /**
     * 获取集合类型
     */
    public static boolean checkListType(String typeName) {
        for (String key : listTypeHandler) {
            if (typeName.startsWith(key+"<") || typeName.equals(key)) {
                return true;
            }
        }
        return false;
    }
    /**
     * 注册Map类型
     */
    public static void putMapType(String typeName) {
        mapTypeHandler.add(typeName);
    }
    /**
     * 获取Map类型
     */
    public static boolean checkMapType(String typeName) {
        for (String key : mapTypeHandler) {
            if (typeName.startsWith(key+"<") || typeName.equals(key)) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * 注册集合类型
     */
    public static void putListType(String typeName) {
        fileTypeHandler.add(typeName);
    }
    
    /**
     * 检查是否忽略对象
     */
    public static boolean checkIgnoreClass(String typeName) {
        for (String key : ignoreClassHandler) {
            Pattern p = Pattern.compile("^"+key+"$",Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(typeName);
            if (m.find()) {
                return true;
            }
        }
        return false;
    }
    /**
     * 注册忽略对象
     */
    public static void putIgnoreClass(String typeName) {
        ignoreClassHandler.add(typeName);
    }
}
